//
//  HttpService.m
//  MyFamework
//
//  Created by carlos on 12-10-1.
//  Copyright (c) 2012年 carlos. All rights reserved.
//

#import "HttpService.h"
#import "ThreadMessage.h"
#import "HttpReceiveHandler.h"
#import "HttpSendHandler.h"

@implementation HttpService

+ (HttpService *)instance{
    static HttpService *instance;
    if (!instance) {
        instance = [[HttpService alloc]init];
    }
    return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        //添加接受前台ui的观察者
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [[NSNotificationCenter defaultCenter]addObserver:self
                                                selector:@selector(reciverFormUI:)
                                                    name:kNSendToHttp
                                                  object:nil];
        [HttpConnect instance].delegate = self;
    }
    return self;
}
- (void)reciverFormUI  :( NSNotification *)notification{
    //    CLog(@"reciverFormUI");
    NSDictionary *dic = [notification userInfo ];
    
    ThreadMessage *threadMessage = [dic valueForKey:kNKeyThreadMessage];
    [self postMsgToHttpSingle:threadMessage];
}
- (void)postMsgToHttpSingle:(ThreadMessage *)threadMessage{
    //    CLog(@"reciverFormUI=%@",threadMessage);
    //异步请求服务器
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(aQueue, ^{
        CLog(@"gotoNewThread");
        HttpEnty *httpEnty = [HttpSendHandler fillThreadMessage:threadMessage];
        [[HttpConnect instance]connent:httpEnty withThreadMessage:threadMessage];
    });
    //获取数据后返回给后台

}
//由外部直接发送threadMsg
+ (void)postMsgToHttp:(ThreadMessage *)threadMessage{
    [[self instance] postMsgToHttpSingle:threadMessage];
}
#pragma mark HttpConnectDelegate
//连接成功
- (void)httpConnectSuccess:(NSString *)content withThreadMessage:(ThreadMessage *)threadMessage{
    
    [HttpReceiveHandler operateReturnThreadMessage:threadMessage withContent:content];
    
    if (threadMessage.receiveUIBlock) {
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{
            CLog(@"返回给前台");
            threadMessage.receiveUIBlock(threadMessage);
        });
        return;
    }
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:threadMessage,kNKeyThreadMessage, nil];
    //使用异步的方式发布广播
    
    [[NSNotificationQueue defaultQueue]enqueueNotification:[NSNotification notificationWithName:threadMessage.returnUINotiKey object:self userInfo:dict] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnSender forModes:nil];
}
//连接失败
- (void)httpConnectFail:(NSError *)error withThreadMessage:(ThreadMessage *)threadMessage{
    //失败的话进入,失败的方法里
    threadMessage.httpError = error;
    
    if (threadMessage.receiveUIBlock) {
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^{
            CLog(@"返回给前台");
            threadMessage.receiveUIBlock(threadMessage);
        });
        return;
    }
    //使用异步的方式发布广播
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:threadMessage,kNKeyThreadMessage, nil];
    [[NSNotificationQueue defaultQueue]enqueueNotification:[NSNotification notificationWithName:threadMessage.returnUINotiKey object:self userInfo:dict] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnSender forModes:nil];
    
}
@end
